﻿using System;
using System.Runtime.Serialization;
using XcoAppSpaces.Contracts.Transport;

namespace XcoAppSpaces.Serialization.Binary
{
	class XcoMessageSerializationSurrogate : ISerializationSurrogate
	{
		public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
		{
			var msg = (XcoMessage)obj;
			info.AddValue("content", msg.Content);
			info.AddValue("causalitycontext", msg.CausalityContext);
			info.AddValue("info", msg.ContentInfo);
		}

		public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
		{
			obj = new XcoMessage
				{
					Content = (byte[]) info.GetValue("content", typeof (byte[])),
					CausalityContext = (byte[]) info.GetValue("causalitycontext", typeof (byte[])),
					ContentInfo = (string) info.GetValue("info", typeof (string)),
				};
			return obj;
		}
	}
}